#include "Reactor.hpp"
#include "Sock.hpp"
#include "Accepter.hpp"

void Usage(std::string s)
{
    std::cout << "Usage: " << s << " port" << std::endl;
}

// ./epoll_server port
int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        exit(1);
    }

    int listen_sock = Sock::Socket();
    uint16_t port = atoi(argv[1]);
    Sock::Bind(listen_sock, port);
    Sock::Listen(listen_sock);
    SetNonBlock(listen_sock);

    // 创建反应堆
    Reactor* R = new Reactor;
    R->InitReactor();

    // 添加燃料
    Event* levp = new Event;
    levp->R = R;
    levp->sock = listen_sock;
    levp->RegisterCallback(Accepter, nullptr, nullptr);
    R->InsertEvent(levp, EPOLLIN|EPOLLET);

    int timeout = -1;
    while(1)
    {
        R->Dispatcher(timeout);
    }
    return 0;
}